package net.deiangi.graphs;

import net.deiangi.graphs.core.Edge;
import net.deiangi.graphs.core.Node;
import net.deiangi.graphs.path.Dijkstra;
import net.deiangi.graphs.ui.GraphPanel;
import net.deiangi.graphs.ui.ProgressReporter;

import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * User: Deian
 * Date: Oct 30, 2009
 * Time: 10:51:07 PM
 */
public class MyGraphApplet extends Applet implements ActionListener, ItemListener {
    static String defaultGraph = "g=\ta-b/60, a-a1/10,a1-a2/10,a2-a3/10,a3-a4/10,a4-a5/10, a5-c/10,\n" + "\tb-c/60, a-c/120, c-d/200, d-e/100, e-f/120, f-g/40,\t \n" + "\td-h/120, h-k/80, k-j/60, k-g/40, g-z/200, j-w/50, w-z/60;\n" + "c=\ta,j,z, c";

    final GraphPanel graphPanel = new GraphPanel(this);

    public void init() {
        setLayout(new BorderLayout());
        add("Center", graphPanel);

        Panel controlPanel = new Panel();
        add("South", controlPanel);

        Panel textPanel = new Panel();
        textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.PAGE_AXIS));
        controlPanel.add(textPanel);

        final TextArea sol = new TextArea(5, 60);
        textPanel.add(sol);

        final TextArea txt = new TextArea(5, 60);
        textPanel.add(txt);
        txt.setText(defaultGraph);

        Panel btnPanel = new Panel();
        controlPanel.add(btnPanel);
        btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.PAGE_AXIS));

        Checkbox cb = new Checkbox("show weights");
        btnPanel.add(cb, BorderLayout.NORTH);
        cb.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                Object src = e.getSource();
                boolean on = e.getStateChange() == ItemEvent.SELECTED;
                graphPanel.graphRenderer.showWeight = on;
            }
        });
        Checkbox cb2 = new Checkbox("show directions");
        btnPanel.add(cb2, BorderLayout.NORTH);
        cb2.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                Object src = e.getSource();
                boolean on = e.getStateChange() == ItemEvent.SELECTED;
                graphPanel.graphRenderer.showDirections = on;
            }
        });
        Checkbox cb3 = new Checkbox("show labels");
        cb3.setState(graphPanel.graphRenderer.showLabels);
        btnPanel.add(cb3, BorderLayout.NORTH);
        cb3.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                Object src = e.getSource();
                boolean on = e.getStateChange() == ItemEvent.SELECTED;
                graphPanel.graphRenderer.showLabels = on;
            }
        });
        Button btn1 = new Button("Re-Load Graph");
        btnPanel.add(btn1, BorderLayout.NORTH);
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                graphPanel.reload(txt.getText());
                graphPanel.repaint();
            }
        });

        Button btn2 = new Button("Walk A->Z");
        btnPanel.add(btn2, BorderLayout.NORTH);
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //                WaveWalker ww = new WaveWalker(graph);
                //                ww.walk("a", "b");
                Dijkstra d = new Dijkstra(graphPanel.graphRenderer.graph);
                d.shortest("a", "z", new ProgressReporter() {
                    public void report(String msg) {
                        sol.setText(sol.getText() + "\n" + msg);
                    }

                    public void noteToNode(Node u, String s) {
                        u.note = s;
                    }

                    public void noteToEdge(Edge v, String s) {
                        v.note = s;
                        v.selected = true;
                    }
                });
            }
        });
    }

    public void start() {
        graphPanel.start();
    }

    public void stop() {
        graphPanel.stop();
    }


    public void actionPerformed(ActionEvent e) {

    }

    public void itemStateChanged(ItemEvent e) {

    }
}

